export const url = 'http://localhost:8080'

const token = wx.getStorageSync('token')

export function axiosAPI(url, data, type) {
  return new Promise(function (resolve, reject) {
    wx.request({
      url: url,
      data: data,
      header: {
        'UserToken': wx.getStorageSync('token')
      },
      method: type,
      success: function (res) {
        if (res.statusCode === 401) {
          if (token) {
            wx.showModal({
              title: '提示',
              content: '登录信息已过期，请重新登录！',
              success(res) {
                if (res.confirm) {
                  wx.switchTab({
                    url: '/pages/my/my',
                  })
                } else if (res.cancel) {}
              }
            })
          } else {
            wx.showModal({
              title: '提示',
              content: '您还未进行登录！',
              success(res) {
                if (res.confirm) {
                  wx.switchTab({
                    url: '/pages/my/my',
                  })
                } else if (res.cancel) {}
              }
            })
          }
        } else {
          resolve(res.data)
        }
      },
      fail: function () {
        reject('网络错误')
        wx.showToast({
          icon: 'error',
          title: '网络错误'
        })
      }
    })
  })
}